<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回当前 Unix 时间戳和微秒数</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.localtime.html">localtime</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.mktime.html">mktime</a></div>
 <div class="up"><a href="ref.datetime.html">Date/Time 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.microtime" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">microtime</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">microtime</span> &mdash; <span class="dc-title">返回当前 Unix 时间戳和微秒数</span></p>

 </div>
 <div class="refsect1 unknown-seealsp" id="refsect1-function.microtime-unknown-seealsp">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>microtime</strong></span>
    ([ <span class="methodparam"><span class="type">bool</span> <code class="parameter">$get_as_float</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   <span class="function"><strong>microtime()</strong></span> 当前 Unix
   时间戳以及微秒数。本函数仅在支持 gettimeofday()
   系统调用的操作系统下可用。
  </p>
  <p class="para">
   如果调用时不带可选参数，本函数以 &quot;msec sec&quot;
   的格式返回一个字符串，其中 sec 是自 Unix 纪元（0:00:00 January 1,
   1970 GMT）起到现在的秒数，msec 是微秒部分。字符串的两部分都是以秒为单位返回的。
  </p>
  <p class="para">
   如果给出了 <em><code class="parameter">get_as_float</code></em> 参数并且其值等价于
   <strong><code>TRUE</code></strong>，<span class="function"><strong>microtime()</strong></span> 将返回一个浮点数。
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    <em><code class="parameter">get_as_float</code></em> 参数是 PHP 5.0.0 新加的。
   </span>
  </p></blockquote>
  <p class="para">
   <div class="example" id="example-2249">
    <p><strong>Example #1 用 <span class="function"><strong>microtime()</strong></span> 对脚本的运行计时</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;Simple&nbsp;function&nbsp;to&nbsp;replicate&nbsp;PHP&nbsp;5&nbsp;behaviour<br />&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">microtime_float</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;list(</span><span style="color: #0000BB">$usec</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sec</span><span style="color: #007700">)&nbsp;=&nbsp;</span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">"&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">microtime</span><span style="color: #007700">());<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;((float)</span><span style="color: #0000BB">$usec&nbsp;</span><span style="color: #007700">+&nbsp;(float)</span><span style="color: #0000BB">$sec</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$time_start&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">microtime_float</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;Sleep&nbsp;for&nbsp;a&nbsp;while<br /></span><span style="color: #0000BB">usleep</span><span style="color: #007700">(</span><span style="color: #0000BB">100</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$time_end&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">microtime_float</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$time&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$time_end&nbsp;</span><span style="color: #007700">-&nbsp;</span><span style="color: #0000BB">$time_start</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Did&nbsp;nothing&nbsp;in&nbsp;</span><span style="color: #0000BB">$time</span><span style="color: #DD0000">&nbsp;seconds\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   参见 <span class="function"><a href="function.time.html" class="function">time()</a></span>。
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.localtime.html">localtime</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.mktime.html">mktime</a></div>
 <div class="up"><a href="ref.datetime.html">Date/Time 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
